<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.1.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"ljmeng.site","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"flat"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":true,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="本文回答下面的问题：  数据库的隔离级别有哪些？ 数据库的隔离级别有什么效果？或者说每个隔离级别解决的问题？ 封锁协议有哪些？ 数据库的隔离级别和封锁协议的关系？">
<meta property="og:type" content="article">
<meta property="og:title" content="封锁协议 和 数据库的隔离级别">
<meta property="og:url" content="http:&#x2F;&#x2F;ljmeng.site&#x2F;posts&#x2F;28475&#x2F;">
<meta property="og:site_name" content="ljmeng的个人小站">
<meta property="og:description" content="本文回答下面的问题：  数据库的隔离级别有哪些？ 数据库的隔离级别有什么效果？或者说每个隔离级别解决的问题？ 封锁协议有哪些？ 数据库的隔离级别和封锁协议的关系？">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http:&#x2F;&#x2F;ljmeng.site&#x2F;posts&#x2F;28475&#x2F;70034e19.png">
<meta property="og:image" content="http:&#x2F;&#x2F;ljmeng.site&#x2F;posts&#x2F;28475&#x2F;5d745f01.png">
<meta property="og:image" content="http:&#x2F;&#x2F;ljmeng.site&#x2F;posts&#x2F;28475&#x2F;e23f91e5.png">
<meta property="article:published_time" content="2019-12-22T05:00:00.000Z">
<meta property="article:modified_time" content="2019-12-26T06:35:30.963Z">
<meta property="article:author" content="Lingjia Meng">
<meta property="article:tag" content="database">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http:&#x2F;&#x2F;ljmeng.site&#x2F;posts&#x2F;28475&#x2F;70034e19.png">

<link rel="canonical" href="http://ljmeng.site/posts/28475/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>封锁协议 和 数据库的隔离级别 | ljmeng的个人小站</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="ljmeng的个人小站" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">ljmeng的个人小站</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">Less is More</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-lab">

    <a href="/lab/" rel="section"><i class="fa fa-flask fa-fw"></i>实验室</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://ljmeng.site/posts/28475/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/uploads/s0mE.jpg">
      <meta itemprop="name" content="Lingjia Meng">
      <meta itemprop="description" content="Less is More">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="ljmeng的个人小站">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          封锁协议 和 数据库的隔离级别
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2019-12-22 13:00:00" itemprop="dateCreated datePublished" datetime="2019-12-22T13:00:00+08:00">2019-12-22</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2019-12-26 14:35:30" itemprop="dateModified" datetime="2019-12-26T14:35:30+08:00">2019-12-26</time>
              </span>

          
            <span id="/posts/28475/" class="post-meta-item leancloud_visitors" data-flag-title="封锁协议 和 数据库的隔离级别" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/posts/28475/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/posts/28475/" itemprop="commentCount"></span>
    </a>
  </span>
  
  <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>2.5k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>2 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>本文回答下面的问题：</p>
<ul>
<li>数据库的隔离级别有哪些？</li>
<li>数据库的隔离级别有什么效果？或者说每个隔离级别解决的问题？</li>
<li>封锁协议有哪些？</li>
<li>数据库的隔离级别和封锁协议的关系？</li>
</ul>
<a id="more"></a>
<h2 id="数据隔离级别概念缘起">数据隔离级别概念缘起</h2>
<p>数据库的基本目标为无论什么情况下，都要保持数据的一致性，而为了达成这一目标，事务处理必须确保ACID性质，即:</p>
<ul>
<li>原子性(Atomic) =&gt; 数据库恢复机制</li>
<li>一致性(Consistency) =&gt; 事务处理的目标</li>
<li>隔离性(Isolation) =&gt; 并发控制机制</li>
<li>持久性(Durability) =&gt; 数据库恢复机制</li>
</ul>
<p><strong>隔离级别</strong>对应的概念正是<strong>隔离性</strong>，所以这是一个与并发控制相关的概念。一个良好的隔离级别应该是每个事务在执行时感觉不到任何其他事务的影响。</p>
<h2 id="数据隔离性应对的问题">数据隔离性应对的问题</h2>
<p>显然当事务并发时会产生很多数据不一致的问题，归结起来有三类不一致性：</p>
<ul>
<li>丢失修改 Lost Update</li>
<li>读脏数据 Dirty Read</li>
<li>不可重复读 Non-repeatable Read</li>
</ul>
<p>但实际操作起来，由于隔离的粒度不同，还有一类不一致性，即：</p>
<ul>
<li>幻读 Phantoms</li>
</ul>
<p>值得提一下的是，幻读和不可重复读的概念很接近，都是两次查询(读取)操作结果不同，而不一样的是：幻读的条件更特殊，幻读指的是记录的条数变了，当记录条数不变，仅仅内容改变时不是幻读。<strong>幻读属于不可重复读的一种情况</strong></p>
<p>例子：</p>
<ul>
<li>某事务第一次查询某人的存款余额是100，之后其他事务修改了余额，再次读取时余额不再是100，这就是不可重复读，但不是幻读。</li>
<li>某事务查询所有存款余额大于100的人，第一次查询只有一个人，之后其他事务插入了另外一个余额大于100的记录，再次查询，变成了两个人，这就是幻读，也是不可重复读。</li>
</ul>
<h2 id="数据库的隔离级别定义">数据库的隔离级别定义</h2>
<p>数据库的隔离级别分为四层，即：</p>
<ul>
<li>串行化 Serializable</li>
<li>可重复读 Repeatable Read</li>
<li>读已提交 Read Committed</li>
<li>读未提交 Read Uncommitted</li>
</ul>
<p>而这四个概念的定义是通过何时释放锁以及何时释放锁来定义的(也有用时间戳机制来描述的): <strong>注意下列加锁操作都是要做什么操作，就加对应操作的锁，比方说读操作就加读锁，而不需要考虑接下来要进行写操作而在读的时候加写锁的情况。</strong></p>
<p><strong>1. 串行化</strong> 最高的隔离级别。对于基于锁的并发控制数据库实现：串行化要求<strong>读锁和写锁(根据所选数据获取)在事务结束的时候释放</strong>。同时当使用带有范围条件<code>where</code>的<code>SELECT</code>语句查询时，要求增加相应的范围锁（例如使用<code>select * from t where val&gt;10 and val &lt; 15</code>查询后，会增加相应的<strong>范围锁</strong>，其他事务插入<code>val</code>在这个范围的数据是不被允许的）。</p>
<p><strong>2.可重复读</strong> 对于基于锁的并发控制数据库实现：要求<strong>读锁和写锁(根据所选数据获取)在事务结束的时候释放</strong>。但是<strong>不管理范围锁</strong>。</p>
<p><strong>3.读已提交</strong> 对于基于锁的并发控制数据库实现：要<strong>写锁(根据所选数据获取)在事务结束的时候释放，读锁在<code>select</code>执行结束后立即释放</strong>。这一级别同样不管理范围锁。</p>
<p><strong>4.读未提交</strong> 对于这一层级，完全不加锁。</p>
<p>到这里我们就可以得出如下结论的表格：</p>
<figure>
<img src="/posts/28475/70034e19.png" alt><figcaption>70034e19.png</figcaption>
</figure>
<p>其实这个表格中一些问题的避免是和封锁方法是对应的：</p>
<ul>
<li>范围锁可以避免<strong>幻读</strong>，</li>
<li>读锁和写锁在事务结束时释放可以避免<strong>丢失修改，不可重复度</strong></li>
<li>写锁在事务结束时释放，申请读锁并在之后释放可以避免<strong>读脏数据</strong></li>
</ul>
<h2 id="数据库封锁协议">数据库封锁协议</h2>
<p>为了定义并发控制的解决策略，引入了串行化和可串行化的概念。为了达到可串行化调度，就有了一系列的控制方法。其中比较好的控制协议就是通过锁的控制来实现的，这就引入了一系列的封锁协议：</p>
<ul>
<li>一级封锁协议</li>
<li>二级封锁协议</li>
<li>三级封锁协议</li>
<li>两阶段封锁协议</li>
<li>一次封锁协议</li>
</ul>
<p>封锁协议是通用的协议，是编程人员编码或者设计的规范。</p>
<p>它们的定义如下:</p>
<blockquote>
<p>一级封锁协议: 事务T在修改数据R之前必须先对其加X锁，直到事务结束才释放。</p>
</blockquote>
<blockquote>
<p>二级封锁协议：一级封锁协议加上事务T在读取数据R之前必须先对其加S锁，读完后方可释放S锁。</p>
</blockquote>
<blockquote>
<p>三级封锁协议：一级封锁协议加上事务T在读取数据R之前必须先对其加S锁，直到事务结束才释放。</p>
</blockquote>
<blockquote>
<p>两阶段封锁协议：① 在对任何数据进行读、写操作之前，首先要申请并获得对该数据的封锁。② 在释放一个封锁之后，事务不再申请和获得其它任何封锁。</p>
</blockquote>
<blockquote>
<p>一次封锁协议： 一次将所有要使用的数据全部加锁</p>
</blockquote>
<p>其中事务结束包括正常结束（COMMIT）和非正常结束（ROLLBACK）</p>
<p>通过定义我们貌似可以发现，封锁协议和数据库隔离级别<strong>看似</strong>有如下对应关系：</p>
<ul>
<li>二级封锁协议 =&gt; 读已提交隔离级别</li>
<li>三级封锁协议 =&gt; 可重复读隔离级别</li>
</ul>
<p><strong>但实际上并不存在对应关系</strong> 根本原因出现在封锁协议中的<strong>在数据修改前加锁</strong>，指的是<code>Read(A)</code>和<code>Write(A)</code>绑定在一起考虑，虽然具体的修改操作发生在<code>Write(A)</code>时，但是在<code>Read(A)</code>的时候就要加上写锁。而数据库的隔离级别不会提前加锁。 另外两者的主体也是不一样的，在隔离层级策略下会出现锁升级的情况（这其实也是因为数据库无法知道操作者接下来的操作是什么导致的，如果知道可能在一开始就会加上写锁保护）。</p>
<h2 id="一些额外的问题">一些额外的问题</h2>
<h3 id="为什么封锁协议和隔离级别看着定义一样实际效果却不同">为什么封锁协议和隔离级别看着定义一样，实际效果却不同？</h3>
<p>在封锁协议中<strong>Read(A)和Write(A)要绑定认为是一个写操作，开始就要加写锁</strong>， 对于之前提到的四种数据一致性问题，我们可以发现：</p>
<ul>
<li>一级封锁协议<del>什么都不能保证</del> 保证不丢失修改</li>
<li>二级封锁协议可以保证不读脏数据 也保证不丢失修改</li>
<li>三级封锁协议则可以保证不读脏数据，可重复读，不丢失修改，<del>但是不能解决幻读</del></li>
</ul>
<p>对于一二级封锁协议的反例如下: <img src="/posts/28475/5d745f01.png" alt="5d745f01.png"> <strong>而这个反例也是不存在的</strong>，因为封锁协议要求T1在<code>Read(A)</code>的时候就要加写锁。</p>
<h3 id="part-2">Part 2</h3>
<p>此外由于存在幻读现象，那么三级封锁协议和两阶段封锁协议是否还是可串行化的？</p>
<p><strong>仍然是可串行化的</strong>，幻读本质是封锁粒度的问题，不过当我们理解封锁协议加锁时，应该更好的理解封锁协议中所说的封锁操作对象的含义，随着访问对象的粒度而调整封锁对象的粒度，比如 <strong><code>Select</code>范围选择时，应该对表加锁</strong>。</p>
<h3 id="part-3">Part 3</h3>
<p>封锁协议之间的包含关系？ <img src="/posts/28475/e23f91e5.png" alt="e23f91e5.png"></p>
<h2 id="参考资料">参考资料</h2>
<blockquote>
<p><a href="https://en.wikipedia.org/wiki/Isolation_(database_systems)" target="_blank" rel="noopener">Isolation (database systems) - Wikipedia</a></p>
</blockquote>
<blockquote>
<p><a href="https://baike.baidu.com/item/%E4%B8%89%E7%BA%A7%E5%8A%A0%E9%94%81%E5%8D%8F%E8%AE%AE/1148391" target="_blank" rel="noopener">三级加锁协议_百度百科</a></p>
</blockquote>

    </div>

    
    
    
      


      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/tags/database/" rel="tag"><i class="fa fa-tag"></i> database</a>
          </div>

        
  <div class="post-widgets">
    <div class="wp_rating">
      <div id="wpac-rating"></div>
    </div>
  </div>


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/posts/63260/" rel="prev" title="Java 与 MySQL 开发环境配置">
      <i class="fa fa-chevron-left"></i> Java 与 MySQL 开发环境配置
    </a></div>
      <div class="post-nav-item">
    <a href="/posts/11894/" rel="next" title="饥荒联机版服务器搭建教程2020版">
      饥荒联机版服务器搭建教程2020版 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#数据隔离级别概念缘起"><span class="nav-number">1.</span> <span class="nav-text">数据隔离级别概念缘起</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#数据隔离性应对的问题"><span class="nav-number">2.</span> <span class="nav-text">数据隔离性应对的问题</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#数据库的隔离级别定义"><span class="nav-number">3.</span> <span class="nav-text">数据库的隔离级别定义</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#数据库封锁协议"><span class="nav-number">4.</span> <span class="nav-text">数据库封锁协议</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#一些额外的问题"><span class="nav-number">5.</span> <span class="nav-text">一些额外的问题</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#为什么封锁协议和隔离级别看着定义一样实际效果却不同"><span class="nav-number">5.1.</span> <span class="nav-text">为什么封锁协议和隔离级别看着定义一样，实际效果却不同？</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#part-2"><span class="nav-number">5.2.</span> <span class="nav-text">Part 2</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#part-3"><span class="nav-number">5.3.</span> <span class="nav-text">Part 3</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#参考资料"><span class="nav-number">6.</span> <span class="nav-text">参考资料</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Lingjia Meng"
      src="/uploads/s0mE.jpg">
  <p class="site-author-name" itemprop="name">Lingjia Meng</p>
  <div class="site-description" itemprop="description">Less is More</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">22</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">22</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">26</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/lingjiameng" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;lingjiameng" rel="noopener" target="_blank"><i class="github fa-fw"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:1574761457@qq.com" title="QQmail → mailto:1574761457@qq.com" rel="noopener" target="_blank"><i class="envelope fa-fw"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:menglingjia0214@gmail.com" title="Gmail → mailto:menglingjia0214@gmail.com" rel="noopener" target="_blank"><i class="google fa-fw"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="https://blog.csdn.net/a3551736" title="CSDN → https:&#x2F;&#x2F;blog.csdn.net&#x2F;a3551736" rel="noopener" target="_blank"><i class="chain fa-fw"></i></a>
      </span>
  </div>


<div id="days"></div>
<script>
function show_date_time(){
window.setTimeout("show_date_time()", 1000);
BirthDay=new Date("1/15/2019 12:00:00");
today=new Date();
timeold=(today.getTime()-BirthDay.getTime());
sectimeold=timeold/1000
secondsold=Math.floor(sectimeold);
msPerDay=24*60*60*1000
e_daysold=timeold/msPerDay
daysold=Math.floor(e_daysold);
e_hrsold=(e_daysold-daysold)*24;
hrsold=setzero(Math.floor(e_hrsold));
e_minsold=(e_hrsold-hrsold)*60;
minsold=setzero(Math.floor((e_hrsold-hrsold)*60));
seconds=setzero(Math.floor((e_minsold-minsold)*60));
document.getElementById('days').innerHTML="已运行 "+daysold+" 天 "+hrsold+" 小时 "+minsold+" 分 "+seconds+" 秒";
}
function setzero(i){
if (i<10)
{i="0" + i};
return i;
}
show_date_time();
</script>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Lingjia Meng</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">84k</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>
  <div class="addthis_inline_share_toolbox">
    <script src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-5df63eb9372b8a11" async="async"></script>
  </div>

        






<script>
  (function() {
    function leancloudSelector(url) {
      url = encodeURI(url);
      return document.getElementById(url).querySelector('.leancloud-visitors-count');
    }

    function addCount(Counter) {
      var visitors = document.querySelector('.leancloud_visitors');
      var url = decodeURI(visitors.id);
      var title = visitors.dataset.flagTitle;

      Counter('get', '/classes/Counter?where=' + encodeURIComponent(JSON.stringify({ url })))
        .then(response => response.json())
        .then(({ results }) => {
          if (results.length > 0) {
            var counter = results[0];
            leancloudSelector(url).innerText = counter.time + 1;
            Counter('put', '/classes/Counter/' + counter.objectId, { time: { '__op': 'Increment', 'amount': 1 } })
              .catch(error => {
                console.error('Failed to save visitor count', error);
              });
          } else {
              Counter('post', '/classes/Counter', { title, url, time: 1 })
                .then(response => response.json())
                .then(() => {
                  leancloudSelector(url).innerText = 1;
                })
                .catch(error => {
                  console.error('Failed to create', error);
                });
          }
        })
        .catch(error => {
          console.error('LeanCloud Counter Error', error);
        });
    }

    function showTime(Counter) {
      var visitors = document.querySelectorAll('.leancloud_visitors');
      var entries = [...visitors].map(element => {
        return decodeURI(element.id);
      });

      Counter('get', '/classes/Counter?where=' + encodeURIComponent(JSON.stringify({ url: { '$in': entries } })))
        .then(response => response.json())
        .then(({ results }) => {
          for (let url of entries) {
            let target = results.find(item => item.url === url);
            leancloudSelector(url).innerText = target ? target.time : 0;
          }
        })
        .catch(error => {
          console.error('LeanCloud Counter Error', error);
        });
    }

    let { app_id, app_key, server_url } = {"enable":true,"app_id":"T4Rf3UmWXRrRET3uJLvw7pkV-gzGzoHsz","app_key":"TDgSBGJrLQavgs9FMBLpw8xl","server_url":null,"security":false};
    function fetchData(api_server) {
      var Counter = (method, url, data) => {
        return fetch(`${api_server}/1.1${url}`, {
          method,
          headers: {
            'X-LC-Id'     : app_id,
            'X-LC-Key'    : app_key,
            'Content-Type': 'application/json',
          },
          body: JSON.stringify(data)
        });
      };
      if (CONFIG.page.isPost) {
        if (CONFIG.hostname !== location.hostname) return;
        addCount(Counter);
      } else if (document.querySelectorAll('.post-title-link').length >= 1) {
        showTime(Counter);
      }
    }

    let api_server = app_id.slice(-9) !== '-MdYXbMMI' ? server_url : `https://${app_id.slice(0, 8).toLowerCase()}.api.lncldglobal.com`;

    if (api_server) {
      fetchData(api_server);
    } else {
      fetch('https://app-router.leancloud.cn/2/route?appId=' + app_id)
        .then(response => response.json())
        .then(({ api_server }) => {
          fetchData('https://' + api_server);
        });
    }
  })();
</script>


      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script>
  <script src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  



  <script>
  if (CONFIG.page.isPost) {
    wpac_init = window.wpac_init || [];
    wpac_init.push({
      widget: 'Rating',
      id    : 18870,
      el    : 'wpac-rating',
      color : 'fc6423'
    });
    (function() {
      if ('WIDGETPACK_LOADED' in window) return;
      WIDGETPACK_LOADED = true;
      var mc = document.createElement('script');
      mc.type = 'text/javascript';
      mc.async = true;
      mc.src = '//embed.widgetpack.com/widget.js';
      var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(mc, s.nextSibling);
    })();
  }
  </script>












  

  
      

<script>
  if (typeof MathJax === 'undefined') {
    window.MathJax = {
      loader: {
          load: ['[tex]/mhchem'],
        source: {
          '[tex]/amsCd': '[tex]/amscd',
          '[tex]/AMScd': '[tex]/amscd'
        }
      },
      tex: {
        inlineMath: {'[+]': [['$', '$']]},
          packages: {'[+]': ['mhchem']},
        tags: 'ams'
      },
      options: {
        renderActions: {
          findScript: [10, doc => {
            document.querySelectorAll('script[type^="math/tex"]').forEach(node => {
              const display = !!node.type.match(/; *mode=display/);
              const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
              const text = document.createTextNode('');
              node.parentNode.replaceChild(text, node);
              math.start = {node: text, delim: '', n: 0};
              math.end = {node: text, delim: '', n: 0};
              doc.math.push(math);
            });
          }, '', false],
          insertedScript: [200, () => {
            document.querySelectorAll('mjx-container').forEach(node => {
              let target = node.parentNode;
              if (target.nodeName.toLowerCase() === 'li') {
                target.parentNode.classList.add('has-jax');
              }
            });
          }, '', false]
        }
      }
    };
    (function () {
      var script = document.createElement('script');
      script.src = '//cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js';
      script.defer = true;
      document.head.appendChild(script);
    })();
  } else {
    MathJax.startup.document.state(0);
    MathJax.texReset();
    MathJax.typeset();
  }
</script>

    

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : false,
      appId      : 'jj8JkPaN8iWC8WvasXt8GPXF-gzGzoHsz',
      appKey     : 'pieyWVlEVwMAiNGtQg93DEGv',
      placeholder: "Just go go",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : 'zh-cn' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

</body>
</html>
